<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/06/02
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;

/**
 * 优惠券使用管理
 * @package app\erp\model
 */
class ErpCouponUseinfo extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_coupon_useinfo';

    /**
     * 查询数据
     * @param id int 优惠券id
     * @author jason
     * @return array_out(code, message, (int)id)
     */
    public function getInfoById($id, $cache = true){
        $info = $this -> cache($cache) -> find($id);
        if($info){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 添加数据
     * @param coupon_id 	            int         折扣券id
     * @param coupon_code 		        string      折扣券编码
     * @param coupon_stime 		        int         折扣券开始时间
	 * @param coupon_etime 		        int         折扣券截止时间
     * @param coupon_getime 			int         折扣券领取时间
     * @param coupon_usetime 			int         折扣券使用时间
     * @param coupon_user 		        int         折扣券领取&使用人
     * @param coupon_ordercode 			int         折扣券使用订单id
     * @param coupon_use_type 	        tinyint     使用状态(0可用;1已绑定;2已使用;3已失效;4不可用)
     * @param coupon_remark 	        text        备注
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function addData($coupon_id, $coupon_code, $coupon_stime, $coupon_etime, $coupon_getime, $coupon_usetime, $coupon_user,  $coupon_ordercode, $coupon_use_type, $coupon_remark=NULL){
        $data['coupon_id']              = $coupon_id;
        $data['coupon_code']            = $coupon_code;
        $data['coupon_stime']           = $coupon_stime;
        $data['coupon_etime']           = $coupon_etime;
        $data['coupon_getime']          = $coupon_getime;
        $data['coupon_usetime']         = $coupon_usetime;
        $data['coupon_user']            = $coupon_user;
        $data['coupon_ordercode']       = $coupon_ordercode;
        $data['coupon_use_type']        = $coupon_use_type;
        $data['coupon_remark']          = $coupon_remark;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
    }


    /**
     * 编辑数据
     * @param coupon_id 	            int         折扣券id
     * @param coupon_code 		        string      折扣券编码
     * @param coupon_stime 		        int         折扣券开始时间
     * @param coupon_etime 		        int         折扣券截止时间
     * @param coupon_getime 			int         折扣券领取时间
     * @param coupon_usetime 			int         折扣券使用时间
     * @param coupon_user 		        int         折扣券领取&使用人
     * @param coupon_ordercode 			int         折扣券使用订单id
     * @param coupon_use_type 	        tinyint     使用状态(0可用;1已绑定;2已使用;3已失效;4不可用)
     * @param coupon_remark 	        text        备注
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
	public function editData($id,$coupon_id, $coupon_code, $coupon_stime, $coupon_etime, $coupon_getime, $coupon_usetime, $coupon_user,  $coupon_ordercode, $coupon_use_type, $coupon_remark=NULL){
		$map = [];
		$map['id'] = $id;
		
		$data = [];
        $data['coupon_id']              = $coupon_id;
        $data['coupon_code']            = $coupon_code;
        $data['coupon_stime']           = $coupon_stime;
        $data['coupon_etime']           = $coupon_etime;
        $data['coupon_getime']          = $coupon_getime;
        $data['coupon_usetime']         = $coupon_usetime;
        $data['coupon_user']            = $coupon_user;
        $data['coupon_ordercode']       = $coupon_ordercode;
        $data['coupon_use_type']        = $coupon_use_type;
        $data['coupon_remark']          = $coupon_remark;
        $res = $this ->update($data, $map);
		if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 删除数据
     * @param int $id
     * @author dou<51779987@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 优惠卷 关联载入（一对一）
     */
    public function getErpCoupon(){
        return $this -> hasOne('app\erp\model\ErpCoupon','id', 'coupon_id');
    }
}
